package com.clothing.controller;

import com.clothing.DateEditor;
import com.clothing.utils.AjaxMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.StringTrimmerEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.util.Date;
import java.util.Set;

public class BaseController {
    @Autowired
    private Validator validator;

    @InitBinder
    protected void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(String.class, new StringTrimmerEditor(true));
        binder.registerCustomEditor(Date.class, new DateEditor(true));
    }

    protected boolean isValidNoMsg(Object target, Class<?>... groups) {
        return validator.validate(target, groups).isEmpty();
    }

    public <T> Set<ConstraintViolation<T>> isValid(T target, Class<?>... groups) {
        return validator.validate(target, groups);
    }

    protected String getParameterErrorMsg() {
        return AjaxMsg.failed("参数错误");
    }
}
